package Network;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

//服务端
/*
建立服务的端口ServerSocket
等待用户的连接accept
接受用户的消息
 */
public class TcpServerDome01 {
    public static void main(String[] args) {
        ServerSocket serverSocket=null;
        Socket socket=null;
        InputStream is=null;
        try {
            //1、我得有一个地址
            serverSocket=new ServerSocket(9999);
            //2、等待客户端连接
            socket=serverSocket.accept();//侦听
            //3、读取服务端的消息
            is=socket.getInputStream();
            byte [] bus=new byte[1024];
            int len;
            while ((len = is.read(bus)) != -1) {
                String str=new String(bus,0,len);
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
